package com.gym.web.controller.handicap.controller;

import java.util.Date;
import java.util.List;

import com.gym.common.core.domain.entity.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gym.common.annotation.Log;
import com.gym.common.enums.BusinessType;
import com.gym.system.handicap.domain.GymHandicap;
import com.gym.system.handicap.service.IGymHandicapService;
import com.gym.common.core.controller.BaseController;
import com.gym.common.core.domain.AjaxResult;
import com.gym.common.utils.poi.ExcelUtil;
import com.gym.common.core.page.TableDataInfo;

/**
 * 报障Controller
 *
 * @author administrator
 * @date 2021-04-11
 */
@Controller
@RequestMapping("/handicap/handicap")
public class GymHandicapController extends BaseController {
    private String prefix = "handicap/handicap";

    @Autowired
    private IGymHandicapService gymHandicapService;

    @RequiresPermissions("handicap:handicap:view")
    @GetMapping()
    public String handicap() {
        return prefix + "/handicap";
    }

    /**
     * 查询报障列表
     */
    @RequiresPermissions("handicap:handicap:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GymHandicap gymHandicap) {
        startPage();
        List<GymHandicap> list = gymHandicapService.selectGymHandicapList(gymHandicap);
        return getDataTable(list);
    }

    /**
     * 导出报障列表
     */
    @RequiresPermissions("handicap:handicap:export")
    @Log(title = "报障", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GymHandicap gymHandicap) {
        List<GymHandicap> list = gymHandicapService.selectGymHandicapList(gymHandicap);
        ExcelUtil<GymHandicap> util = new ExcelUtil<GymHandicap>(GymHandicap.class);
        return util.exportExcel(list, "handicap");
    }

    /**
     * 新增报障
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存报障
     */
    @RequiresPermissions("handicap:handicap:add")
    @Log(title = "报障", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GymHandicap gymHandicap) {
        if (StringUtils.isBlank(gymHandicap.getHandicapUser())) {
            SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
            gymHandicap.setHandicapUser(sysUser.getUserName());
        }
        if (gymHandicap.getStartTime() == null) {
            gymHandicap.setStartTime(new Date());
        }
        return toAjax(gymHandicapService.insertGymHandicap(gymHandicap));
    }

    /**
     * 修改报障
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GymHandicap gymHandicap = gymHandicapService.selectGymHandicapById(id);
        mmap.put("gymHandicap", gymHandicap);
        return prefix + "/edit";
    }

    /**
     * 修改保存报障
     */
    @RequiresPermissions("handicap:handicap:edit")
    @Log(title = "报障", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GymHandicap gymHandicap) {
        return toAjax(gymHandicapService.updateGymHandicap(gymHandicap));
    }

    /**
     * 删除报障
     */
    @RequiresPermissions("handicap:handicap:remove")
    @Log(title = "报障", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gymHandicapService.deleteGymHandicapByIds(ids));
    }
}
